cap program drop bytype_bycomp

program define bytype_bycomp

syntax varlist, ctrls(string) condition(string) outcome(string) xmin(string) xmax(string) xdelta(string) wave(string)

	foreach depvar in `varlist' {

	local depvar `depvar'
	local ctrls `ctrls'
	local condition `condition'
	local outcome `outcome'
	local xmin `xmin'
	local xmax `xmax'
	local xdelta `xdelta'
	local wave `wave'

		reg `depvar' treat_sbd_mix treat_dw_mix treat_sbd_hom_blue treat_dw_hom_blue `ctrls' if `condition'
			margins, dydx(treat_sbd_mix treat_dw_mix treat_sbd_hom_blue treat_dw_hom_blue)
				matrix `depvar' = r(table)
					matselrc `depvar' `depvar'_sbd_mix, r(1, 5, 6) c(1)
					matselrc `depvar' `depvar'_dw_mix, r(1, 5, 6) c(2)
					matselrc `depvar' `depvar'_sbd_hom, r(1, 5, 6) c(3)
					matselrc `depvar' `depvar'_dw_hom, r(1, 5, 6) c(4)
	
				graph set window fontface "Times"
					coefplot (matrix(`depvar'_sbd_mix[1,]), ci((`depvar'_sbd_mix[2,] `depvar'_sbd_mix[3,])) recast(scatter) ciopts(lcolor(black) lpattern(solid)) mcolor(black) msymbol(square)) ///
						(matrix(`depvar'_sbd_hom[1,]), ci((`depvar'_sbd_hom[2,] `depvar'_sbd_hom[3,])) recast(scatter) ciopts(lcolor(black) lpattern(longdash)) mcolor(black) msymbol(circle)) ///
						(matrix(`depvar'_dw_mix[1,]), ci((`depvar'_dw_mix[2,] `depvar'_dw_mix[3,])) recast(scatter) ciopts(lcolor(black) lpattern(dash)) mcolor(black) msymbol(triangle)) ///
						(matrix(`depvar'_dw_hom[1,]), ci((`depvar'_dw_hom[2,] `depvar'_dw_hom[3,])) recast(scatter) ciopts(lcolor(black) lpattern(shortdash)) mcolor(black) msymbol(diamond)), ///
						name(`depvar', replace) scheme(plotplain) ///
						xtitle("Treatment effect on `outcome'", margin(medsmall)) xlabel(`xmin'(`xdelta')`xmax', nogrid) ///
						ytitle("", margin(medsmall)) ylabel(, nogrid notick nolabels) ///
						legend(on nostack position(6) cols(2)order(2 "Mixed SBD" 4 "Homogeneous SBD" 6 "Mixed DW" 8 "Homogeneous DW")) ///
						xline(0, lcolor(black) lpattern(dash)) 

							graph export "Output/`depvar'_bytype_bycomp_`wave'.pdf", as(pdf) replace
		cap graph export "$dir_Overleaf/Figures/`depvar'_bytype_bycomp_`wave'.pdf", as(pdf) replace
		cap graph export "$dir_Overleaf_JG/Figures/`depvar'_bytype_bycomp_`wave'.pdf", as(pdf) replace

	
	}
	
end
		
		
